import React from 'react'
import PropTypes from 'prop-types'
import { Button } from 'antd'
import { routerRedux } from 'dva/router'
import { connect } from 'dva'
import { Page } from 'components'
import queryString from 'query-string'
import List from './List'
import Filter from './Filter'
import Modal from './Modal'
import ConfigModal from './ConfigModal'

const Group = ({
  location, dispatch, group, loading,
}) => {
  location.query = queryString.parse(location.search)
  const { query, pathname } = location
  const {
    list, pagination, users, groups, currentItem, modalVisible, configModalVisible,
  } = group
  const { pageSize } = pagination

  const showModal = () => {
    dispatch({
      type: 'group/showModal',
    })
  }

  const modalProps = {
    item: currentItem,
    visible: modalVisible,
    maskClosable: false,
    confirmLoading: loading.effects['group/save'],
    title: currentItem.id ? '编辑分组' : '添加分组',
    wrapClassName: 'vertical-center-modal',
    onOk (data) {
      dispatch({
        type: 'group/save',
        payload: data,
      })
    },
    onCancel () {
      dispatch({
        type: 'group/hideModal',
      })
    },
  }

  const configModalProps = {
    item: currentItem,
    visible: configModalVisible,
    users,
    groups,
    dispatch,
    maskClosable: false,
    confirmLoading: loading.effects['group/info'],
    title: '配置用户',
    wrapClassName: 'vertical-center-modal',
    onOk (data) {
      dispatch({
        type: 'group/config',
        payload: data,
      })
    },
    onCancel () {
      dispatch({
        type: 'group/hideConfigModal',
      })
    },
  }

  const listProps = {
    dataSource: list,
    loading: loading.effects['group/query'],
    pagination,
    location,
    onChange (page) {
      dispatch(routerRedux.push({
        pathname,
        search: queryString.stringify({
          ...query,
          page: page.current,
          pageSize: page.pageSize,
        }),
      }))
    },
    onDeleteItem (param) {
      dispatch({
        type: 'group/delete',
        payload: param,
      })
    },
    onConfigItem (param) {
      dispatch({
        type: 'group/info',
        payload: param,
      })
    },
    onEditItem (param) {
      dispatch({
        type: 'group/showModal',
        payload: {
          currentItem: param,
        },
      })
    },
  }

  const filterProps = {
    filter: {
      ...query,
    },
    onFilterChange (value) {
      dispatch(routerRedux.push({
        pathname: location.pathname,
        search: queryString.stringify({
          ...value,
          page: 1,
          pageSize,
        }),
      }))
    },
    onSearch (fieldsValue) {
      fieldsValue.keyword.length ? dispatch(routerRedux.push({
        pathname: '/group',
        query: {
          field: fieldsValue.field,
          keyword: fieldsValue.keyword,
        },
      })) : dispatch(routerRedux.push({
        pathname: '/group',
      }))
    },
    onAdd () {
      dispatch({
        type: 'group/showModal',
        payload: {
          modalType: 'create',
        },
      })
    },
    switchIsMotion () {
      dispatch({ type: 'group/switchIsMotion' })
    },
  }

  return (
    <Page inner>
      <Filter {...filterProps} />
      <Button type="primary" style={{ marginBottom: '10px' }} onClick={showModal}>添加分组</Button>
      <List {...listProps} />
      {modalVisible && <Modal {...modalProps} />}
      {configModalVisible && <ConfigModal {...configModalProps} />}
    </Page>
  )
}

Group.propTypes = {
  group: PropTypes.object,
  location: PropTypes.object,
  dispatch: PropTypes.func,
  loading: PropTypes.object,
}

export default connect(({ group, loading }) => ({ group, loading }))(Group)
